package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author SNA
 * @date 2024/2/6
 **/
@Tag(name = "用户接口")  // 用于swagger接口文档，描述接口作用。为了等一下接口的测试中有中文的提示
@RestController  // 交给spring管理，返回JSON格式数据。
@RequestMapping(value = "/admin/system/index")  // 请求路径
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    //生成图片验证码
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }


    // 用户登录
    @Operation(summary = "登录的方法")
    @PostMapping("login")  // 提交方式：post提交，请求路径：/admin/system/index/login
    public Result login(@RequestBody LoginDto loginDto){  //RequestBody注解，表示接收JSON格式数据。通过JSON格式传递
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo() {
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);

    }

    // 获取当前登录用户信息
//    @GetMapping(value = "/getUserInfo")
//    public Result getUserInfo(@RequestHeader(name = "token") String token) {
//        //1、从请求头获取token，有两种方法。第一种是在参数为HttpServletRequest request，第二种是通过@RequestHeader注解获取
////        String token = request.getHeader("token");
//        //2、根据token查询Redis，获取用户信息
//        SysUser sysUser = sysUserService.getUserInfo(token);
//
//        //3、返回用户信息
//        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
//    }

    //用户退出
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


}
